package com.example.rednest.serviceImpl;

import com.example.rednest.mapper.NewsMapper;
import com.example.rednest.pojo.News;
import com.example.rednest.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper = null;

    //根据id查询新闻
    @Override
    @Transactional
    //@Cacheable(value = "redisRednest",key = "'redisNews'+#id")
    public News selectNewsById(int id) {
        return newsMapper.selectNewsById(id);
    }

    @Override
    @Transactional
    @CachePut(value = "redisRednest",key = "'redisNews'+#id")
    public News insertNews(News news) {
        newsMapper.insertNews(news);
        return news;
    }

    @Override
    @Transactional
    @CachePut(value = "redisRednest",key = "'redisNews'+#news.id" ,
            condition = "#result != 'null'")
    public News updataNews(int id,News news) {
        newsMapper.updataNews(news);
        return news;
    }

    @Override
    @Transactional
    @CacheEvict (value = "redisRednest",key = "'redisNews'+#news.id")
    public int deleteNews(int id) {
        return newsMapper.deleteNews(id);
    }
}
